package io.jee.medusa.orm.utils;

import java.lang.reflect.Method;
import java.util.Map;

public class EntityUtils {

	public static <T> T change(Map<String, Object> map, Class<T> clz) {
		T entity = null;
		try {
			entity = clz.newInstance();
			String first;
			String last;
			String set = "set";
			String methodName;
			Method method;
			Class<?> type;
			Map<String, Method> setters = ClassUtils.getAllSetterMethods(clz);
			for (String key : map.keySet()) {
				first = key.substring(0, 1).toUpperCase();
				last = key.substring(1);
				methodName = set + first + last;
				method = setters.get(methodName);
				if (method != null) {
					type = method.getParameterTypes()[0];
					method.invoke(entity, type.cast(map.get(key)));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entity;
	}
	
}
